//
//  CircleView.m
//  ChartsDemo
//
//  Created by apple on 2021/3/29.
//

#import "SingleView.h"

/* 弧度转角度 */
#define SK_RADIANS_TO_DEGREES(radian) \
        ((radian) * (180.0 / M_PI))
/* 角度转弧度 */
#define SK_DEGREES_TO_RADIANS(angle) \

@interface SingleView ()

@property(nonatomic,strong) UIColor *e_color;
@property(nonatomic,strong) CAGradientLayer *gradientLayer1;

@end


@implementation SingleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        [UIColor colorWithString:@"#1DA1BE"]
        self.e_color = [UIColor colorWithString:@"#0095B6"];
        CGFloat startAngle = -M_PI*7/6; // 起始角度
        CGFloat endAngle = -M_PI*7/6+M_PI*4/3; // 结束角度
    
        CAShapeLayer *outLayer=[CAShapeLayer layer];
        outLayer.frame = self.bounds;
        [self.layer addSublayer:outLayer];
        outLayer.fillColor= [UIColor clearColor].CGColor;
        outLayer.strokeColor = [UIColor colorWithString:@"#EFF1F5"].CGColor;
        outLayer.opacity = 1;
        outLayer.lineCap = kCALineCapRound;
        outLayer.lineWidth = 9;
        
        UIBezierPath *outPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width/2, frame.size.height/2) radius:frame.size.width/2-6 startAngle:startAngle endAngle:endAngle clockwise:YES];
        outLayer.path = [outPath CGPath];
        _circleLayer = [CAShapeLayer layer];
        _circleLayer.frame = self.bounds;
        _circleLayer.fillColor = [[UIColor clearColor] CGColor];
        _circleLayer.strokeColor = [UIColor whiteColor].CGColor;
        _circleLayer.lineCap = kCALineCapRound;
        _circleLayer.lineWidth = 9;
        _circleLayer.path =[outPath CGPath];
        _circleLayer.strokeEnd = 0.0;
        
        CALayer *layer1 = [CALayer layer];
        self.gradientLayer1 = [CAGradientLayer layer];
        self.gradientLayer1.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        self.gradientLayer1.backgroundColor = self.e_color.CGColor;
        [layer1 addSublayer:self.gradientLayer1];
        [layer1 setMask:_circleLayer];
        [self.layer addSublayer:layer1];

        [self addSubViews];
    }
    return self;
}

-(void)setPercent:(CGFloat)percent animated:(BOOL)animated{
    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    _circleLayer.strokeEnd = percent/240.0;
    [CATransaction commit];
}

- (void)addSubViews{
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.text = @"0";
    self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
    self.titleLab.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    self.tipLab = [[UILabel alloc] init];
    self.tipLab.text = @"PI";
    self.tipLab.textColor = [UIColor colorWithString:@"#9a9a9a"];
    self.tipLab.font = [UIFont systemFontOfSize:13];
    [self addSubview:self.tipLab];
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.titleLab);
        make.top.equalTo(self.titleLab.mas_bottom).offset(10);
    }];
    
    self.leftLab = [[UILabel alloc] init];
    self.leftLab.textColor = [UIColor colorWithString:@"#9a9a9a"];
    self.leftLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.leftLab];
    [self.leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-15);
        make.left.equalTo(self).offset(-5);
    }];
    
    self.rightLab = [[UILabel alloc] init];
    self.rightLab.textColor = [UIColor colorWithString:@"#9a9a9a"];
    self.rightLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.rightLab];
    [self.rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.leftLab);
        make.right.equalTo(self).offset(10);
    }];
}


@end
